package tuna;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class UserOp {	
	private String utorId;
	private boolean isProf = false;
	private int userId;
	
	public UserOp(String utorId) {
		this.utorId = utorId;
	}
	
	public UserOp(String utorId, int userId) {
		this.utorId = utorId;
		setUserId(userId);
	}
	
	public UserOp(String utorId, String role, int userId) {
		this.utorId = utorId;
		setUserId(userId);
		
		if(role.equals("student")) {
			setProf(false);
		}
		else {
			setProf(true);
		}
	}
	
	public void setProf(boolean isProf) {
		this.isProf = isProf;
	}

	public boolean isProf() {
		return isProf;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getUserId() {
		return userId;
	}
	
	
	public boolean isUserExist(String pwd) {
		//Create DB connection & connect
        DBConnection dbCon = new DBConnection("localhost:3306", "tuna", "root", "asdf1234");
        dbCon.connectDB();
        
        //Retrieve result set
        String query = "select id, utorId, pwd, isProf from userRecord where utorId=\'" + utorId + "\' and pwd=\'" + pwd + "\'";
        ResultSet user = dbCon.runQuery(query);
        
        boolean isUserExist = false;        
        
        try {
			if(user.next()) {
				isUserExist = true;
				setUserId(user.getInt(1));
				setProf(user.getBoolean(4));
			}
			else {
				isUserExist = false;
			}
		} 
        catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			isUserExist = false;
		}
        
        dbCon.disconnectDB();
        
        return isUserExist;
	}
	
	public boolean submitRating(String userId, String profId, String pres, String comm, 
			String exp, String teach, String work, String learn) {
		DBConnection dbCon = new DBConnection("localhost:3306", "tuna", "root", "asdf1234");
		dbCon.connectDB();
		
		String query = "select id from rateRecord where userId=" + userId +" and profId=" + profId;
		
		ResultSet existRating = dbCon.runQuery(query);
		
		boolean success = false;
		
		try {		
			//Rating created by the user already exists
			if(existRating.next()) {
				query = "update rateRecord set pres=" + pres + ", comm=" + comm + ", " +
						"exp=" + exp + ", teach=" + teach +", work=" + work + ", learn=" + learn + 
						" where profId=" + profId + " and userId=" + userId;
			}
			//Rating does not exist
			else {	
				query = "insert into rateRecord (userId, profId, pres, comm, exp, teach, work, learn) " +
						"values (" + userId + ", " + profId + ", " + pres + ", " + comm + ", " + 
						exp + ", " + teach + ", " + work + ", " + learn + ")";
			}
			
			//Insert or update accordingly
			success = dbCon.updateQuery(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		}
		
		dbCon.disconnectDB();
		
		return success;
	}
	
	public int getUserIdFromSql() {
		DBConnection dbCon = new DBConnection("localhost:3306", "tuna", "root", "asdf1234");
		dbCon.connectDB();
		
		String query = "select id from userRecord where utorId=\'" + utorId + "\'";
		
		ResultSet idResult = dbCon.runQuery(query);
		int id = -1;
		
		try {
			if(idResult.next()) {
				id = idResult.getInt(1);
			}
		} 
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		dbCon.disconnectDB();
		
		return id;
	}
	
	public boolean sendMessage(int senderId, int receiverId, String message) {
		DBConnection dbCon = new DBConnection("localhost:3306", "tuna", "root", "asdf1234");
		dbCon.connectDB();
		
		String format = "yyyy-MM-dd HH:mm:ss";
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(format);
	    
	    String sentAt = sdf.format(cal.getTime());

		String query = "insert into msgRecord (senderId, receiverId, sentAt, detail) values (" + 
			senderId + ", " + receiverId + ", \'" + sentAt + "\', \'" + message + "\')";
		
		boolean success = dbCon.updateQuery(query);
		
		dbCon.disconnectDB();
		
		return success;
	}

	public boolean sendPost(String message) {
		DBConnection dbCon = new DBConnection("localhost:3306", "tuna", "root", "asdf1234");
		dbCon.connectDB();
		
		String format = "yyyy-MM-dd HH:mm:ss";
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(format);
	    
	    String writtenAt = sdf.format(cal.getTime());

		String query = "insert into postRecord (userId, writtenAt, detail) values (" + 
			userId + ", \'" + writtenAt + "\', \'" + message + "\')";
		
		boolean success = dbCon.updateQuery(query);
		
		dbCon.disconnectDB();
		
		return success;
	}
}
